extends Node

## 全局敌人管理器，处理隐身状态等
var player_is_stealthed: bool = false
var stealth_duration: float = 0.0

func _ready() -> void:
	# 订阅隐身事件
	EventBus.subscribe("player_stealth_start", on_player_stealth_start)
	EventBus.subscribe("player_stealth_end", on_player_stealth_end)

## 玩家开始隐身
func on_player_stealth_start() -> void:
	player_is_stealthed = true
	print("玩家进入隐身状态")

## 玩家结束隐身
func on_player_stealth_end() -> void:
	player_is_stealthed = false
	stealth_duration = 0.0
	print("玩家退出隐身状态")

## 检查玩家是否处于隐身状态
func is_player_stealthed() -> bool:
	return player_is_stealthed

## 获取最近的敌人（包括召唤物）
func get_nearest_enemy_to_player() -> Node:
	var enemies = get_tree().get_nodes_in_group("enemy")
	var nearest_enemy = null
	var nearest_distance = 1000.0
	
	for enemy in enemies:
		var distance = Utils.player.global_position.distance_to(enemy.global_position)
		if distance < nearest_distance:
			nearest_distance = distance
			nearest_enemy = enemy
	
	return nearest_enemy

## 获取最近的玩家单位（包括召唤物）
func get_nearest_player_unit_to_enemy(enemy_position: Vector2) -> Node:
	var player_units = []
	
	# 添加玩家
	if Utils.player:
		player_units.append(Utils.player)
	
	# 添加召唤物（如果有的话）
	var summons = get_tree().get_nodes_in_group("summon")
	player_units.append_array(summons)
	
	var nearest_unit = null
	var nearest_distance = 1000.0
	
	for unit in player_units:
		var distance = enemy_position.distance_to(unit.global_position)
		if distance < nearest_distance:
			nearest_distance = distance
			nearest_unit = unit
	
	return nearest_unit 
